import axios from 'axios'
const request = axios.create({
    baseURL: '/api',
    timeout: 5000,
    headers: {
        'Content-Type': 'application/json'
    }
})

// 请求拦截器
request.interceptors.request.use(
    (config) => {
        const token = localStorage.getItem('token')
        if (token) {
            config.headers['Authorization'] = `Bearer ${token}`
        }
        return config
    },
    (error) => Promise.reject(error)
)

// 响应拦截器（保持原有逻辑）
request.interceptors.response.use(
    (response) => response.data,
    (error) => {
        const status = error.response?.status;
        switch (status) {
            case 401: alert('token无效，请重新登录'); break;
            case 404: alert('请求地址错误'); break;
            case 500: alert('服务器错误'); break;
            default: alert('未知错误');
        }
        return Promise.reject(error)
    }
)

export default request